import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.GroupLayout;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import com.conf.MenuEnum;
import com.jcomponent.MyMenu;
import com.log.LogMonitor;
import com.log.LogMonitorFactroy;
import com.log.Event.LogEvent;
import com.log.listener.LogListener;
import com.services.GenCES;

/**
 * Title: <br>
 * Description: <br>
 * Copyright: WangGuangya<br>
 * Company: WangGuangya<br>
 * 
 * @author WangGuangya
 * @version 1.0
 * @date 2018-02-23
 */
public class Main {
	private JFrame frame;
	JTextArea log = new JTextArea();

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Main window = new Main();
					window.frame.setResizable(false);
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public Main() {
		initialize();
	}

	private void initialize() {
		frame = new JFrame();
		frame.setTitle("对公评级系统日终");
		frame.setBounds(300, 100, 500, 600);
		frame.getContentPane().setLayout(null);

		createTextPane();
		ceateMenu();

		frame.setDefaultCloseOperation(3);
	}

	private void ceateMenu() {
		JMenuBar menuBar = new JMenuBar();
		frame.setJMenuBar(menuBar);
		JMenu mnStg = new JMenu("STG1");
		menuBar.add(mnStg);
		JMenu mnStg_1 = new JMenu("STG3");
		menuBar.add(mnStg_1);
		
		addMenu(mnStg);
		addMenu(mnStg_1);

		JMenu mnStg_2 = new JMenu("修改日期");
		menuBar.add(mnStg_2);

		JMenuItem menuItem = new JMenuItem("修改日期");
		menuItem.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				SwitchDate.show();
			}
		});
		mnStg_2.add(menuItem);

		addAllMenuListener(mnStg, "STG1");
		addAllMenuListener(mnStg_1, "STG3");
	}
	
	private void addMenu(JMenu parent) {
		for (MenuEnum e : MenuEnum.values()) {
			if (null == e.parent || "".equals(e.parent)) {
				if (e.type != null && "JMenu".equals(e.type)) {
					JMenu menu = new JMenu(e.name, true);
					parent.add(menu);
					addSubMenu(menu, e.id);
				} else {
					parent.add(new MyMenu(e));
				}
			}
		}
	}
	
	private void addSubMenu(JMenu parent, String parentId) {
		for (MenuEnum e : MenuEnum.getEnums(parentId)) {
			if (e.type != null && "JMenu".equals(e.type)) {
				JMenu menu = new JMenu(e.name, true);
				parent.add(menu);
				addSubMenu(menu, e.id);
			} else {
				parent.add(new MyMenu(e));
			}
		}
	}

	private void addAllMenuListener(JMenuItem parent, String name) {
		if ((parent instanceof JMenu)) {
			JMenu menu = (JMenu) parent;
			for (int i = 0; i < menu.getItemCount(); i++)
				addAllMenuListener(menu.getItem(i), name);
		} else {
			parent.setName(name);
			parent.addActionListener(new MyMenuListener());
		}
	}

	private void createTextPane() {
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(27, 33, 447, 487);
		scrollPane.setHorizontalScrollBarPolicy(31);
		scrollPane.setVerticalScrollBarPolicy(20);
		GroupLayout groupLayout = new GroupLayout(frame.getContentPane());
		groupLayout.setHorizontalGroup(groupLayout.createParallelGroup(
				GroupLayout.Alignment.LEADING).addGroup(
				groupLayout.createSequentialGroup().addContainerGap()
						.addComponent(scrollPane).addContainerGap()));

		groupLayout.setVerticalGroup(groupLayout.createParallelGroup(
				GroupLayout.Alignment.LEADING).addGroup(
				groupLayout.createSequentialGroup().addContainerGap()
						.addComponent(scrollPane, -1, 230, 32767)
						.addContainerGap()));

		log.setLineWrap(true);
		log.setEditable(false);
		scrollPane.setViewportView(log);
		frame.getContentPane().add(scrollPane);
		frame.getContentPane().setLayout(groupLayout);
	}

	private class MyMenuListener implements ActionListener {
		private MyMenuListener() {
		}

		public void actionPerformed(ActionEvent e) {
			SwitchDate.hide();
			String source = "";
			MyMenu item = (MyMenu) e.getSource();
			source = item.getName();

			try {
				GenCES ces = new GenCES(source);
				final LogMonitor logMonitor = LogMonitorFactroy.newInstance();
				logMonitor.setLogChangedListener(new LogListener() {
					public void EventActivated(LogEvent me) {
						log.setText(logMonitor.toString());
						log.setCaretPosition(log.getText().length());
						log.paintImmediately(log.getBounds());
					}
				});
				ces.generalStep(item.getMenuEnum());
			} catch (Exception e1) {
				e1.printStackTrace();
				log.setText(e1.getMessage());
			}
		}
	}
}